﻿using QLBH.Models;
using QLBH.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.Modules
{
    public partial class frmAdd_Edit_Good : Form
    {
        private eType eType;
        private GoodModel model;
        public GoodModel Model
        {
            get { return model; }
            set { model = value; }
        }

        public frmAdd_Edit_Good(eType eType)
        {
            InitializeComponent();
            this.eType = eType;
            Model = new GoodModel();
        }

        private void LoadDataCategory()
        {
            List<CategoryModel> category = WSAccess.GetData<List<CategoryModel>>(MainForm.Instance.Shop_id.ToString() + "/categories");
            cboCategori.DataSource = category;
            cboCategori.DisplayMember = "name";
            cboCategori.ValueMember = "id";
            List<UnitsModel> unit = WSAccess.GetData<List<UnitsModel>>(MainForm.Instance.Shop_id.ToString() + "/units");
            cbbUnit.DataSource = unit;
            cbbUnit.DisplayMember = "name";
            cbbUnit.ValueMember = "id";
        }
        private void frmAdd_Edit_Good_Load(object sender, EventArgs e)
        {
            LoadDataCategory();
            if (this.eType == eType.ADD)
            {
                this.Text = "Thêm Mặt Hàng";
            }
            else
            {
                this.Text = "Sửa Mặt Hàng";
                //load data griview to textbox
                txtCode.Text = Model.Code;
                txtName.Text = Model.Name;
                cboCategori.SelectedValue = Model.Category_id;
                dtExpirate.Text = Model.Expirate;
                txtPrice_import.Text = Model.Import_price.ToString();
                txtWholeSalePrice.Text = Model.Wholesale_prices.ToString();
                txtRetailPrice.Text = Model.Retail_price.ToString();
                txtWare.Text = Model.Ware;
                cbbUnit.SelectedValue = Model.Unit_id;
                rtbDescription.Text = Model.Description;
            }
        }

        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(txtCode.Text, "Mã Vạch", 30);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCode.SelectAll();
                txtCode.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtName.Text, "Tên Mặt Hàng", 50);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDateTime(DateTime.Parse(dtExpirate.Text), "Hạn Sử Dụng");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtExpirate.Select();
                dtExpirate.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(txtPrice_import.Text,"Giá Nhập");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPrice_import.SelectAll();
                txtPrice_import.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(txtWholeSalePrice.Text, "Giá Bán Buôn");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtWholeSalePrice.SelectAll();
                txtWholeSalePrice.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(txtRetailPrice.Text, "Giá Bán Lẻ");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRetailPrice.SelectAll();
                txtRetailPrice.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtWare.Text, "Xuất Xứ", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtWare.SelectAll();
                txtWare.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(rtbDescription.Text, "Ghi Chu", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Mặt Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rtbDescription.SelectAll();
                rtbDescription.Focus();
                return false;
            }
            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                Model.Code = txtCode.Text;
                Model.Name = txtName.Text;
                Model.Category_id = Int32.Parse(cboCategori.SelectedValue.ToString());
                DateTime date = DateTime.Parse(dtExpirate.Text);
                Model.Expirate = date.ToString("yyyy-MM-dd");
                Model.Import_price = Double.Parse(txtPrice_import.Text);
                Model.Wholesale_prices = Double.Parse(txtWholeSalePrice.Text);
                Model.Retail_price = Double.Parse(txtRetailPrice.Text);
                Model.Ware = txtWare.Text;
                Model.Unit_id = Int32.Parse(cbbUnit.SelectedValue.ToString());
                Model.Description = rtbDescription.Text;
                //check dieu kien truoc khi chay
                if (eType == eType.ADD)
                {
                    try
                    {
                        GoodInsertResponse result = WSAccess.Insert<GoodModel, GoodInsertResponse>(model, MainForm.Instance.Shop_id + "/good/insert"); ;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        WSAccess.Update<GoodModel>(model, Model.Id + "/good/edit");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                this.DialogResult = DialogResult.None;
            }
        }
    }
    public class GoodInsertResponse : ResponseBase
    {
        private int good_id;

        public int Good_id
        {
            get { return good_id; }
            set { good_id = value; }
        }
    }
}
